package dk.viauc.cmclang.intlang;

import dk.viauc.cmclang.intlang.ast.IntegerLiteral;

public class Parser implements IParser {
/*
	private IScanner scan;

	private Token currentTerminal;

	public Parser(IScanner scan) {
		this.scan = scan;
		currentTerminal = scan.scan();
	}

	public Object parseProgram() {
		parseBlock();

		return null;
	}

	private void parseBlock() {
		accept(Token.FUNCTION);
		parseName();
		accept(Token.KINDOF);
		accept(Token.IDENTIFIER);
		if (currentTerminal.kind == Token.WITHPARAMS) {
			accept(Token.WITHPARAMS);
			parseDeclaration();
		}
		accept(Token.BEGIN);

		parseStatement();

		accept(Token.ENDFUNCTION);

	}

	private void parseName() {
		if (currentTerminal.kind == Token.MAIN) {
			accept(Token.MAIN);
		} else if (currentTerminal.kind == Token.LABEL) {
			accept(Token.LABEL);
		}
	}

	private void parseIdentifier() {

	}

	private void parseDeclaration() {
		while (currentTerminal.kind == Token.LET) {
			parseOneDeclaration();
		}
	}

	private void parseOneDeclaration() {
		switch (currentTerminal.kind) {
		case Token.LET:
			accept(Token.LET);
			parseType();

			break;
		default:
			System.out.println("let expected");
			break;
		}
	}

	private void parseType() {
		accept(Token.LABEL);
		accept(Token.KINDOF);
		accept(Token.IDENTIFIER);
		// parseIdentifier();// ?????????????????????????????????///
		if (currentTerminal.kind == Token.ARRAYOF) {
			accept(Token.ARRAYOF);
			while (currentTerminal.kind == Token.INTEGERLITERAL) {
				parseLiteral();
			}

		}
		if (currentTerminal.kind == Token.BE) {
			accept(Token.BE);
			if (currentTerminal.kind == Token.INTEGERLITERAL) {
				while (currentTerminal.kind == Token.INTEGERLITERAL) {
					parseLiteral();
				}
			} else if (currentTerminal.kind == Token.SIZEOF) {
				parsePrimary();

			}
		}
	}

	private void parseStatement() {
		while (currentTerminal.kind == Token.LABEL
				|| currentTerminal.kind == Token.INTEGERLITERAL
				|| currentTerminal.kind == Token.SIZEOF
				|| currentTerminal.kind == Token.IF
				|| currentTerminal.kind == Token.WHILE
				|| currentTerminal.kind == Token.READ
				|| currentTerminal.kind == Token.WRITE
				|| currentTerminal.kind == Token.LET) {
			switch (currentTerminal.kind) {
			case Token.LET:
				parseOneDeclaration();
				break;

			case Token.LABEL:
			case Token.INTEGERLITERAL:
			case Token.SIZEOF:
				parseExpression();

				break;

			case Token.IF:
				accept(Token.IF);
				parseExpression();
				accept(Token.DO);

				parseStatement();
				if (currentTerminal.kind == Token.ELSE) {
					accept(Token.ELSE);
					//accept(Token.DO);

					parseStatement();
				}
				accept(Token.ENDIF);

				break;

			case Token.WHILE:
				accept(Token.WHILE);
				parseExpression();
				accept(Token.DO);

				parseStatement();
				accept(Token.ENDWHILE);
				break;

			case Token.READ:
				accept(Token.READ);
				parseExpression();

				break;

			case Token.WRITE:
				accept(Token.WRITE);
				parseExpression();
				break;

			default:
				System.out.println("Error in statement");
				break;
			}
		}
	}

	private void parseExpression() {
		if (currentTerminal.kind == Token.LET) {
			parseOneDeclaration();
		} else {
			parsePrimary();
		}
		while (currentTerminal.kind == Token.OPERATOR) {
			accept(Token.OPERATOR);
			parsePrimary();
		}
	}

	private void parsePrimary() {
		switch (currentTerminal.kind) {
		case Token.LABEL:
			accept(Token.LABEL);
			while (currentTerminal.kind == Token.OPERATOR
					|| currentTerminal.kind == Token.AT
					|| currentTerminal.kind == Token.BE) {

				if (currentTerminal.kind == Token.OPERATOR) {
					accept(Token.OPERATOR);
					parseName();
				}

				else if (currentTerminal.kind == Token.AT) {
					accept(Token.AT);
					accept(Token.INTEGERLITERAL);
					while (currentTerminal.kind == Token.OPERATOR) {
						accept(Token.OPERATOR);
						parseExpression();
					}

				} else if (currentTerminal.kind == Token.BE) {
					accept(Token.BE);
					parsePrimary();
				}
			}
			break;

		case Token.INTEGERLITERAL:
			accept(Token.INTEGERLITERAL);
			break;

		case Token.SIZEOF:
			accept(Token.SIZEOF);
			parseName();
			break;

		default:
			System.out.println("Error in primary");
			break;
		}
	}

	private void parseLiteral() {
		if (currentTerminal.kind == Token.INTEGERLITERAL) {
			IntegerLiteral res =new IntegerLiteral(currentTerminal.spelling);
			accept(Token.INTEGERLITERAL);
		}
			

	}

	private void accept(byte expected) {
		if (currentTerminal.kind == expected)

			currentTerminal = scan.scan();

		else
			System.out
					.println("Expected token of kind " + expected
							+ " current kind:" + currentTerminal.kind
							+ " and current spelling["
							+ currentTerminal.spelling + "]");

		if (currentTerminal.kind == Token.ENDFUNCTION)
			System.out
					.println("ENDFUNCTION found, program parsed without errors.");
	}
*/
	@Override
	public Object parseProgram() {
		// TODO Auto-generated method stub
		return null;
	}
	
}
